import * as z from 'zod';
import type { FormSchema } from '../types';

// 性别选项
const genderOptions = [
  { label: '男', value: 1 },
  { label: '女', value: 2 },
];

// 状态选项
const statusOptions = [
  { label: '正常', value: 1 },
  { label: '禁用', value: 0 },
];

// 角色选项
const roleOptions = [
  { label: '管理员', value: 'admin' },
  { label: '用户', value: 'user' },
];

// 用户表单 Schema
export const userFormSchema: FormSchema = {
  config: {
    fields: [
      {
        name: 'name',
        type: 'text',
        label: '姓名',
        placeholder: '请输入姓名',
        required: true,
        grid: { span: 1 },
      },
      {
        name: 'email',
        type: 'email',
        label: '邮箱',
        placeholder: '请输入邮箱',
        required: true,
        grid: { span: 1 },
      },
      {
        name: 'position',
        type: 'text',
        label: '职位',
        placeholder: '请输入职位',
        grid: { span: 1 },
      },
      {
        name: 'phone',
        type: 'tel',
        label: '电话',
        placeholder: '请输入电话号码',
        grid: { span: 1 },
      },
      {
        name: 'gender',
        type: 'select',
        label: '性别',
        placeholder: '请选择性别',
        options: genderOptions,
        grid: { span: 1 },
      },
      {
        name: 'status',
        type: 'select',
        label: '状态',
        placeholder: '请选择状态',
        options: statusOptions,
        grid: { span: 1 },
      },
      {
        name: 'role',
        type: 'select',
        label: '角色',
        placeholder: '请选择角色',
        options: roleOptions,
        grid: { span: 1 },
      },
      {
        name: 'bio',
        type: 'textarea',
        label: '个人简介',
        placeholder: '请输入个人简介',
        grid: { span: 2 },
      },
    ],
    layout: {
      columns: 2,
      spacing: 'space-y-4',
    },
    submitButton: {
      text: '保存',
    },
    cancelButton: {
      text: '取消',
      show: true,
    },
  },
  validationSchema: z.object({
    id: z.string().optional(),
    name: z.string().min(2, '姓名至少2个字符').max(50, '姓名最多50个字符'),
    email: z.string().email('请输入有效的邮箱地址'),
    position: z.string().optional(),
    phone: z.string().optional(),
    gender: z.number().optional(),
    status: z.number(),
    bio: z.string().optional(),
    role: z.string().optional(),
  }),
  initialValues: {
    name: '',
    email: '',
    position: '',
    phone: '',
    gender: undefined,
    status: 1,
    bio: '',
    role: 'user',
  },
};

// 创建用户表单 Schema（不包含 id 字段）
export const createUserFormSchema: FormSchema = {
  ...userFormSchema,
  validationSchema: z.object({
    name: z.string().min(2, '姓名至少2个字符').max(50, '姓名最多50个字符'),
    email: z.string().email('请输入有效的邮箱地址'),
    position: z.string().optional(),
    phone: z.string().optional(),
    gender: z.number().optional(),
    status: z.number(),
    bio: z.string().optional(),
    role: z.string().min(1, '角色不能为空'),
  }),
};

// 编辑用户表单 Schema（包含 id 字段）
export const editUserFormSchema: FormSchema = {
  ...userFormSchema,
  validationSchema: z.object({
    id: z.string().min(1, '用户ID不能为空'),
    name: z.string().min(2, '姓名至少2个字符').max(50, '姓名最多50个字符'),
    email: z.string().email('请输入有效的邮箱地址'),
    position: z.string().optional(),
    phone: z.string().optional(),
    gender: z.number().optional(),
    status: z.number(),
    bio: z.string().optional(),
    role: z.string().min(1, '角色不能为空'),
  }),
};
